package com.example.demo.controller;

import com.example.demo.bean.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    //普通的form表单(不加enctype属性即application/x-www-form-urlencoded)的get/post提交,它们都提交string类型的数据到后端，都不用@RequestBody
    ////application/x-www-form-urlencoded就是在表单提交时如果是get就把表单拼成？xx=&xx=的格式
    //form下的属性enctype就是axios传值的时候设置的几种content-type!!!!!!
    @RequestMapping("/FormGetPost")
    public User FormGetPost(User user) {
        return user;
    }

    //普通的axios的get提交，默认application/x-www-form-urlencoded,get提交不用@RequestBody
    //application/x-www-form-urlencoded就是在表单提交时如果是get就把表单拼成？xx=&xx=的格式
    @RequestMapping("/AxiosGet")//只要封装了属性即便只传输一个username也可以取到
    public User AxiosGet(User user) {
        return user;
    }

    @RequestMapping("/AxiosPostForm")
    //普通的axios的post提交form(form标签),content-type默认application/json，用@RequestBody
    public User AxiosPostForm(@RequestBody User user) {
        return user;
    }

    @RequestMapping("/AxiosPostDataform")
    //axios的post提交axios定义的data(这里是form),content-type为multipart/form-data，不用@RequestBody
    public User AxiosPostDataform(User user) {
        return user;
    }

    @RequestMapping("/AxiosPostDatafile")
    //axios的post提交axios定义的data(这里是file),content-type为multipart/form-data，不用@RequestBody,但是因为要取到名字要加@RequestParam("pic")
    public String AxiosPostDatafile(@RequestParam("pic") MultipartFile uploadFile) {
        String str = userService.pictureUpload(uploadFile);
        if (!str.equals("false")) {
            return str;   //最后返回访问的url,这个图片服务器host是自己的虚拟机
        } else {
            return "上传失败!";
        }
    }

}
